document.addEventListener('DOMContentLoaded', function () {
    // 回到顶部按钮
    const backToTopButton = document.getElementById('back-to-top');

    window.addEventListener('scroll', function () {
        backToTopButton.style.display = window.pageYOffset > 300 ? 'block' : 'none';
    });

    backToTopButton.addEventListener('click', function (e) {
        e.preventDefault();
        window.scrollTo({
            top: 0,
            behavior: 'smooth'
        });
    });

    // 评论表单提交处理
    const commentForm = document.getElementById('comment-form');
    if (commentForm) {
        commentForm.addEventListener('submit', function (e) {
            e.preventDefault();

            const name = document.getElementById('name').value.trim();
            const commentText = document.getElementById('comment').value.trim();

            if (name && commentText) {
                const commentsList = document.getElementById('comments-list');
                const now = new Date();
                const dateStr = now.toLocaleDateString('zh-CN');

                const newComment = document.createElement('div');
                newComment.className = 'comment';

                newComment.innerHTML = `
          <div class="comment-author">
            <img src="https://via.placeholder.com/50" alt="用户头像">
            <span></span>
          </div>
          <div class="comment-content">
            <p></p>
            <div class="comment-meta">${dateStr}</div>
          </div>
        `;

                newComment.querySelector('span').textContent = name;
                newComment.querySelector('p').textContent = commentText;

                commentsList.appendChild(newComment);
                commentForm.reset();
                newComment.scrollIntoView({ behavior: 'smooth' });
            }
        });
    }

    // 联系表单提交处理
    const contactForm = document.querySelector('.contact-form');
    if (contactForm) {
        contactForm.addEventListener('submit', function (e) {
            e.preventDefault();

            const name = contactForm.querySelector('input[type="text"]').value.trim();
            const email = contactForm.querySelector('input[type="email"]').value.trim();
            const message = contactForm.querySelector('textarea').value.trim();

            if (name && email && message) {
                alert(`感谢您的留言，${name}！我们会尽快回复您。`);
                contactForm.reset();
            } else {
                alert('请填写所有必填字段！');
            }
        });
    }

    // 订阅表单提交处理
    const subscribeForm = document.querySelector('.subscribe-form');
    if (subscribeForm) {
        subscribeForm.addEventListener('submit', function (e) {
            e.preventDefault();

            const email = subscribeForm.querySelector('input[type="email"]').value.trim();

            if (email) {
                alert(`感谢订阅！我们已将确认邮件发送至 ${email}`);
                subscribeForm.reset();
            } else {
                alert('请输入有效的邮箱地址！');
            }
        });
    }
});
